package org.dizem.intellipainter.component.shape;

import org.dizem.intellipainter.model.ShapeType;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * User: dizem@126.com
 * Time: 12-2-18 下午6:12
 */
public class ShapeButton extends JToggleButton {
	private final ShapeType shapeType;
	private static final Dimension SIZE = new Dimension(35, 35);
	private static final Font font = new Font("微软雅黑", Font.PLAIN, 13);

	public ShapeButton(final ShapeType shapeType, final ShapeSelectCallback callback) {
		this.shapeType = shapeType;
		setDoubleBuffered(true);

		addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				callback.onSelectShape(shapeType);
			}
		});
		setToolTipText("画" + shapeType.getText());

	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		g.setFont(font);
		Graphics2D g2d = (Graphics2D) g;
		RenderingHints qualityHints = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		qualityHints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
		g2d.setRenderingHints(qualityHints);
		g.setColor(Color.GRAY);
		switch (shapeType) {
			case Line:
				g.drawLine(10, 10, 25, 25);
				break;
			case Rect:
				g.drawRect(8, 8, 19, 19);
				break;
			case Circle:
				g.drawOval(8, 8, 20, 20);
				break;
			case Point:
				g.fillOval(15, 15, 5, 5);
				break;
			case FillRect:
				g.fillRect(8, 8, 19, 19);
				break;
			case FillCircle:
				g.fillOval(8, 8, 20, 20);
				break;
		}
	}

	@Override
	public Dimension getPreferredSize() {
		return SIZE;
	}

	public ShapeType getShapeType() {
		return shapeType;
	}
}
